//
//  EditPhoneNumberTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/21.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "EditPhoneNumberTableViewController.h"
#import "HYNetHelper.h"
#import <JFBase/JFBase.h>
#import "JFAreaCodeTableViewController.h"
#import "AWUserInfo.h"

@interface EditPhoneNumberTableViewController ()

@end

@implementation EditPhoneNumberTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = JFMainLocalizedString(@"myProfile.updatePhone");
    
    self.countryCode.AWplaceholder(JFMainLocalizedString(@"myProfile.country-code"));
    self.countryCodeTitleLable.text = JFMainLocalizedString(@"myProfile.country-code");
    
    self.phone.AWplaceholder(JFMainLocalizedString(@"myProfile.phone-number"));
    self.phoneNumberTitleLable.text=JFMainLocalizedString(@"myProfile.phone-number");
    
    self.smsCode.AWplaceholder(JFMainLocalizedString(@"myProfile.sms-code"));
    self.smsCodeTitleLable.text= JFMainLocalizedString(@"myProfile.sms-code");
    
    self.sureBtn.AWtitle(JFMainLocalizedString(@"confirm"), UIControlStateNormal);
    
    
    
    [self.smsCode addSendButton];
    WEAKSELF
    self.smsCode.block = ^{
        [weakSelf senderSMS];
    };
    

}

#pragma mark - Table view data source
-(void)senderSMS{
    WEAKSELF
    [HYNetHelper verifyMobile:self.phone.text countryCode:self.countryCode.text entity:@"User" callBack:^(JFResultVo *resultVo) {
        if (resultVo.errorNo != 0) {
            [SLRemindView showMessage:JFMainLocalizedString(@"transactions.failed") toView:weakSelf.view];
        }
    }];
}

- (IBAction)sureAction:(UIButton*)sender {
    WEAKSELF
    if (self.countryCode.text && self.phone.text && self.countryCode.text) {
        [HYNetHelper usersPatch:self.countryCode.text mobile:self.phone.text mobile_verification_code:self.smsCode.text callBack:^(JFResultVo *resultVo) {
            if (resultVo.errorNo == 0) {
              [SLRemindView showMessage:JFMainLocalizedString(@"transactions.success") toView:weakSelf.view];
                User().mobile = weakSelf.phone.text;
                [HYNetHelper allUsers:^(JFResultVo *resultVo) {
                    if (resultVo.errorNo == 0) {
                        [User() initWithDic:resultVo.results];
                    }
                }];
            }else{
                [SLRemindView showMessage:JFMainLocalizedString(@"transactions.failed") toView:weakSelf.view];
            }
            
        }];
    }else{
         [SLRemindView showMessage:JFMainLocalizedString(@"transactions.failed") toView:self.view];
    }

}

- (IBAction)selectCodeAction:(id)sender {
    JFAreaCodeTableViewController *vc = [[JFAreaCodeTableViewController alloc]init];
    vc.returnCountryCodeBlock = ^(NSString *areaCodeStr) {
        self.countryCode.text = areaCodeStr;
    };
    [self.navigationController pushViewController:vc animated:YES];
}



@end
